package Linklist.linklist.two;

import java.awt.event.MouseWheelListener;

public class doubleLink {
    static class linknode{
        private int val;
        private linknode prve;
        private linknode next;
        public linknode(int val){
            this.val=val;
        }
    }
    private linknode head;
    private linknode last;
    // 2、无头双向链表实现
        //头插法
        public void addFirst(int data){
            linknode node=new linknode(data);
            if (head==null){
                head=node;
                last=node;
            }else {
                node.next=head;
                head.prve=node;
                head=node;
            }
        }
        //尾插法
        public void addLast(int data){
            linknode node=new linknode(data);
            if(last==null){
                last=node;
                head=node;
            }else {
                last.next=node;
                node.prve=last;
                last=node;
            }
        }
        //任意位置插入,第一个数据节点为0号下标
        public void addIndex(int index,int data){
            linknode cur=head;
            linknode node=new linknode(data);
            if (index==0){
                addFirst(data);
            }
            if(index==size()){
                addLast(data);
            }
            while (index!=0){
                cur=cur.next;
                index--;
            }
            node.next=cur;
            cur.prve.next=node;
            node.prve=cur.prve;
            cur.prve=node;
        }
        //查找是否包含关键字key是否在单链表当中
        public boolean contains(int key){
            linknode cur=head;
            while (cur!=null){
                if (cur.val==key){
                    return true;
                }
                cur=cur.next;
            }
            return false;
        }
        //删除第一次出现关键字为key的节点
        public void remove(int key){
            linknode cur=head;
            if(head.val==key){
                head=head.next;
                return;
            }
            while (cur!=null){
                if (cur.val==key){
                    cur.prve.next=cur.next;
                    cur.next.prve=cur.prve;
                    return;
                }
                cur=cur.next;
            }
        }
        //删除所有值为key的节点
        public void removeAllKey(int key){
            linknode cur=head.next;
            linknode nowCur=cur;
            while (cur!=null){
                while (cur.val==key){
                    nowCur.prve.next=cur.next;
                    cur.next.prve=nowCur.prve;
                    cur=cur.next;
                }
                cur=cur.next;
                nowCur=cur;
            }
            if (head.val==key){
                head=head.next;
            }
        }
        //得到单链表的长度
        public int size(){
            int index=0;
            linknode cur=head;
            if(head==null){
                return 0;
            }
            while (cur!=null){
                cur=cur.next;
                index++;
            }
            return index;
        }
        public void display(){
            linknode cur=head;
            while (cur!=null){
                System.out.print(cur.val+" ");
                cur=cur.next;
            }
        }
        public void create(){
            linknode node1=new linknode(23);
            linknode node2=new linknode(23);
            linknode node3=new linknode(34);
            linknode node4=new linknode(23);
            linknode node5=new linknode(56);
            node1.next=node2;
            node2.next=node3;
            node3.next=node4;
            node4.next=node5;

            node5.prve=node4;
            node4.prve=node3;
            node3.prve=node2;
            node2.prve=node1;

            this.last=node5;

            this.head=node1;
        }
        public void clear(){}

}
